package sk.patt.eshop.pages.admin;

import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitButton;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.HeadersToolbar;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.NavigationToolbar;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import sk.patt.eshop.CategoriesProvider;
import sk.patt.eshop.SubcategoriesProvider;
import sk.patt.eshop.data.dao.interfaces.CategoryDao;
import sk.patt.eshop.data.dao.interfaces.SubcategoryDao;
import sk.patt.eshop.data.dataobjects.Category;
import sk.patt.eshop.data.dataobjects.Event;
import sk.patt.eshop.data.dataobjects.Subcategory;

public class AdminSubcategoriesPage extends AdminPage {

	@SpringBean
	private SubcategoryDao subcategoryDao;
		
//	private TextField<String> subcategoryTitle;
//	private SubcategoriesProvider subcategoriesProvider = new SubcategoriesProvider(subcategoryDao);
	
	public AdminSubcategoriesPage() {
		Form<Subcategory> subcategoryForm = new Form<Subcategory>("insertSubcategoryForm",new CompoundPropertyModel<Subcategory>(new Subcategory()));
		subcategoryForm.add(new TextField<String>("title").setRequired(true));
				
final WebMarkupContainer wmc = new WebMarkupContainer("data");
wmc.setOutputMarkupId(true);
add(wmc);

		PageableListView listview = new PageableListView<Subcategory>("rows",new PropertyModel<List<Subcategory>>(this, "subcategoryDao.findAll"),10) {

			@Override
			protected void populateItem(ListItem<Subcategory> item) {
				final Subcategory subcategory = item.getModelObject();
				item.add(new Label("id", subcategory.getId()+""));
				item.add(new Label("name", subcategory.getTitle()));
				item.add(new AjaxLink("remove") {

					@Override
					public void onClick(AjaxRequestTarget target) {
						subcategoryDao.delete(subcategory);
//						target.addComponent(wmc);
//						System.out.println("You are about remove: "+subcategory.getTitle());
					}
				});
				
			}
		};

		wmc.add(listview);
		wmc.add(new AjaxPagingNavigator("navigator", listview));
		wmc.setVersioned(false);
				
		subcategoryForm.add(new AjaxSubmitButton("submit") {
			
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				Subcategory subcategory = (Subcategory) form.getModelObject();
				Subcategory newSubcategory = new Subcategory();
				newSubcategory.setTitle(subcategory.getTitle());
				subcategoryDao.save(newSubcategory);
				target.addComponent(wmc);
				
			}
		}); 
		
		add(subcategoryForm);
	}
}
